import requests
import asyncio
from typing import Dict, List, Optional

class TrafficJamService:
    
    @staticmethod
    def get_road_rank_traffic_jam_data(road_type: int = 0, time_type: int = 0, city_code: str = "420100") -> Dict:
        """
        获取道路拥堵数据
        
        Args:
            road_type (int): 道路类型 0-全部道路 1-快速/高速公路 2-普通道路
            time_type (int): 时间类型 0-实时 1-昨天
            city_code (str): 城市代码，默认武汉420100
            
        Returns:
            Dict: 包含道路拥堵数据的字典
        """
        
        try:
            params = {
                "roadType": road_type,
                "timeType": time_type,
                "cityCode": city_code
            }

            url = "https://report.amap.com/ajax/roadRank.do"

            response = requests.get(url, params=params, timeout=10)
            response.raise_for_status()
            
            data = response.json()
            
            return {
                "success": True,
                "data": data
            }
                
        except requests.Timeout:
            print("请求超时")
            return {
                "success": False,
                "message": "请求超时，请稍后重试"
            }
        except requests.RequestException as e:
            print(f"请求错误: {str(e)}")
            return {
                "success": False,
                "message": "网络请求失败"
            }
        except Exception as e:
            print(f"获取数据时发生错误: {e}")
            return {"error": str(e)}
        

    @staticmethod
    def get_road_detail(road_type:int, time_type:int, city_code:str, line_code:str) -> Dict:
        """
        获取道路详情

        Args:
            road_type (int): 道路类型 0-全部道路 1-快速/高速公路 2-普通道路
            time_type (int): 时间类型 0-实时 1-昨天
            city_code (str): 城市代码 默认武汉420100
            line_code (str): 道路代码

        Returns:
            Dict: 包含道路详情的字典

        """

        try:
            params = {
                "roadType": road_type,
                "timeType": time_type,
                "cityCode": city_code,
                "lineCode": line_code
            }

            print(params)

            url = "https://report.amap.com/ajax/roadDetail.do"

            response = requests.get(url, params=params, timeout=10)
            response.raise_for_status()
            
            data = response.json()
            
            return {
                "success": True,
                "data": data
            }
        
        except requests.Timeout:
            print("请求超时")
            return {
                "success": False,
                "message": "请求超时，请稍后重试"
            }
        except requests.RequestException as e:
            print(f"请求错误: {str(e)}")
            return {
                "success": False,
                "message": "网络请求失败"
            }
        except Exception as e:
            print(f"获取数据时发生错误: {e}")
            return {"error": str(e)}